package dongze.com.ckdj.service.uploader;

import android.util.Log;

import java.util.List;

import dongze.com.ckdj.dagger.component.ServiceComponent;
import dongze.com.ckdj.data.DataLayer;
import dongze.com.ckdj.map.MapUtil;
import dongze.com.ckdj.persistence.dao.DaoSession;
import dongze.com.ckdj.persistence.dao.MassLossUploadRecord;
import dongze.com.ckdj.persistence.dao.MassLossUploadRecordDao;
import dongze.com.ckdj.persistence.dao.ServerUploadTask;
import dongze.com.ckdj.persistence.dao.ServerUploadTaskDao;
import dongze.com.ckdj.util.MyLog;
import io.reactivex.Scheduler;


/**
 * Created by fanqwz on 2017/8/21.
 */

public class UploaderFactory {
    public static final String TAG = UploaderFactory.class.getSimpleName();
    public static final String TYPE_IMAGE = "image";
    public static final String TYPE_MASS_LOSS = "massloss";
    public static final String TYPE_NAME_PLATE = "nameplate";
    public static final String TYPE_OUT_LINE = "outline";
    public static final String TYPE_RANDOM_INSPECTION = "randomInspection";
    public static final String TYPE_MARTIN_CHECK_IN = "martin_check_in";

    public static Uploader getUploader(Scheduler subscribeThread, Scheduler observerThread, MapUtil mapUtil, DataLayer dataLayer, ServerUploadTask task, DaoSession session, ImageUploader.ImageProgressListener listener) {
        try {
            if (TYPE_MASS_LOSS.equals(task.getType())) {
                return getMassLossUploader(subscribeThread, observerThread, mapUtil, dataLayer, task, session, listener);
            } else if (TYPE_NAME_PLATE.equals(task.getType())
                    || TYPE_OUT_LINE.equals(task.getType())
                    || TYPE_RANDOM_INSPECTION.equals(task.getType())) {
                return getNamePlateUploader(subscribeThread, observerThread, mapUtil, dataLayer, task, session, listener);
            } else if (TYPE_MARTIN_CHECK_IN.equals(task.getType())) {
                return getMartinCheckInUploader(subscribeThread, observerThread, mapUtil, dataLayer, task, session, listener);
            } else if (TYPE_IMAGE.equals(task.getType())) {
                return getImageUploader(subscribeThread, observerThread, mapUtil, dataLayer, task, session, listener);
            }
        } catch (Exception e) {
            MyLog.e(TAG, "getUploader: ", e);
            e.printStackTrace();
        }
        return new DummyUploader(subscribeThread, observerThread);
    }

    public static void deleteTaskRecord(DaoSession daoSession, long recordID, String type) {
        if (TYPE_MASS_LOSS.equals(type)) {
            daoSession.getMassLossUploadRecordDao().deleteByKey(recordID);
        } else if (TYPE_NAME_PLATE.equals(type)
                || TYPE_OUT_LINE.equals(type)
                || TYPE_RANDOM_INSPECTION.equals(type)) {
            daoSession.getNamePlateUploadRecordDao().deleteByKey(recordID);
        } else if (TYPE_IMAGE.equals(type)) {

        }
    }
    private static MassLossUploader getMassLossUploader(Scheduler subscribeThread, Scheduler observerThread, MapUtil mapUtil, DataLayer dataLayer, ServerUploadTask task, DaoSession session, ImageUploader.ImageProgressListener listener) throws Exception {
        return new MassLossUploader(subscribeThread, observerThread, task, mapUtil, dataLayer, session, listener);
    }

    private static NamePlateUploader getNamePlateUploader(Scheduler subscribeThread, Scheduler observerThread, MapUtil mapUtil, DataLayer dataLayer, ServerUploadTask task, DaoSession session, ImageUploader.ImageProgressListener listener) throws Exception {
        return new NamePlateUploader(subscribeThread, observerThread, task, mapUtil, dataLayer, session, listener);
    }

    private static MartinImageCheckInUploader getMartinCheckInUploader(Scheduler subscribeThread, Scheduler observerThread, MapUtil mapUtil, DataLayer dataLayer, ServerUploadTask task, DaoSession session, ImageUploader.ImageProgressListener listener) throws Exception {
        return new MartinImageCheckInUploader(subscribeThread, observerThread, task, mapUtil, dataLayer, session, listener);
    }

    private static ImageUploader getImageUploader(Scheduler subscribeThread, Scheduler observerThread, MapUtil mapUtil, DataLayer dataLayer, ServerUploadTask task, DaoSession session, ImageUploader.ImageProgressListener listener) throws Exception {
        return new ImageUploader(subscribeThread, observerThread, dataLayer, session.getImageDao(), task.getImage(), listener);
    }


    public static String getTypeText(String type) {
        if (UploaderFactory.TYPE_NAME_PLATE.equals(type)) {
            return "铭牌";
        } else if (UploaderFactory.TYPE_OUT_LINE.equals(type)) {
            return "车身";
        } else if (UploaderFactory.TYPE_RANDOM_INSPECTION.equals(type)) {
            return "商检抽查";
        } else if (UploaderFactory.TYPE_IMAGE.equals(type)) {
            return "图片";
        } else if (UploaderFactory.TYPE_MASS_LOSS.equals(type)) {
            return "质损";
        } else {
            return "未知类型";
        }
    }
}
